package com.zegome.game.gomestory.main.scene.vs;
import java.util.Random;

import aurelienribon.tweenengine.TweenManager;

import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.scenes.scene2d.Actor;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.scenes.scene2d.ui.Image;
import com.badlogic.gdx.scenes.scene2d.ui.ImageButton;
import com.badlogic.gdx.scenes.scene2d.ui.Label;
import com.badlogic.gdx.scenes.scene2d.ui.Table;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener;
import com.badlogic.gdx.scenes.scene2d.utils.ChangeListener.ChangeEvent;
import com.zegome.facebook.IFacebookBridge;
import com.zegome.game.entity.sprite.Sprite;
import com.zegome.game.gomestory.main.helper.IMAction;
import com.zegome.game.gomestory.main.scene.BaseGameScreen;
import com.zegome.game.gomestory.main.scene.play.board.Board;
import com.zegome.game.handler.IUpdateHandler;
import com.zegome.game.main.AndroidGameController;
import com.zegome.game.screen.GameScreenOption;

public class VS2Screen extends BaseGameScreen {

	private Sprite bg;
	private Board userBoard, bossBoard;
	IUpdateHandler check;
	boolean isPlaying;
	public static int LEVEL;
	
	
	public VS2Screen(AndroidGameController controller, String name, final IFacebookBridge bridge) {
		super(controller, name,bridge);		
		setConfig(true, false, true,true);		
	}

	public void initGame(){
		userBoard = new Board(this,false,true,false,(440),(425-125),(110),(480-425));
		bossBoard = new Board(this,false,true,true,(20*10),(6*20),(580),(480-250));
	}
		
	public void show() {
		ctr.a.soundOnOff();
		ctr.a.isHasDialog.removeAllElements();
		ctr.d.backLis.removeAllElements();
		initGame();
		state = 0;
		stage.clear();
		createUi();
		startGame();
		super.show();
		
	}
	
	public void clear(){
		super.clear();
		removeChild(bg);
		bg = null;
		if(userBoard!=null)
		userBoard.clear();
		userBoard = null;
		if(bossBoard!=null)
		bossBoard.clear();
		bossBoard = null;
		isPlaying = false;		
		clearUpdateHandler();		
		check = null;
	}
	
	public void startGame(){
		isPlaying = true;
		userBoard.start(LEVEL);
		bossBoard.start(LEVEL);
		isFinishGame = false;
		isWaitResult = false;
		ctr.d.vs2ResultAction = null;
	}
	
	public void randLevel(boolean isInVS2){
		ctr.d.getDataChapter();
		int chapterMax=1;
		for(int i=0;i<8;i++){
		if(ctr.d.chapters[i].isUnLock == true){
			chapterMax = i;
		}
		}
		
		chapterMax+=1;
		
		if(ctr.d.isLDPI){
		if(chapterMax>=5 || chapterMax >=6){
			if(chapterMax >= 7){
				LEVEL = Math.abs(new Random().nextInt(chapterMax*20))+1;
				while (LEVEL>=81 && LEVEL <=120) {
					LEVEL = Math.abs(new Random().nextInt(chapterMax*20))+1;
				}				
			}
			else{
				LEVEL = Math.abs(new Random().nextInt(4*20))+1;
			}
		}
		else{		
			LEVEL = Math.abs(new Random().nextInt(chapterMax*20))+1;
		}
		}
		else{			
			LEVEL = Math.abs(new Random().nextInt(chapterMax*20))+1;
		}
		
		if(ctr.d.isNoelWeek){
			LEVEL = 141 + Math.abs(new Random().nextInt(20));
		}
		
		
		if(isInVS2){
			if(ctr.d.getChapter(LEVEL)==ctr.a.currentChapter){						
			}
			else{												
				ctr.a.loadRSGame(LEVEL, true);
				getController().finishLoading();
				ctr.a.loadedRSGame(LEVEL, true);	
			}
		}
	}
	
	boolean isFinishGame;
	boolean isWaitResult;
	
	@Override
	public void onManagedUpdate(float pDeltaTime) {
		// TODO Auto-generated method stub
		if(isFinishGame)
		{			
			if(ctr.d.vs2ResultAction!=null && isWaitResult){
				isWaitResult = false;
//				postRunnable(new Runnable() {
//					
//					@Override
//					public void run() {
						// TODO Auto-generated method stub
						ctr.d.vs2ResultAction.action();
//					}
//				});
			}
		}
		if(moneyStr!=null)
		moneyStr.setText(" "+ctr.d.gomeB);
		update(pDeltaTime);		
		super.onManagedUpdate(pDeltaTime);
	}
	
	public void createUi() {		
		if(check == null){
			check = new IUpdateHandler() {				
				@Override
				public void reset() {
					// TODO Auto-generated method stub					
				}				
				@Override
				public void onUpdate(float arg0) {
					// TODO Auto-generated method stub
					if (isPlaying) {
						if (((userBoard.dataMap.total == 0 && userBoard.dataMap.stylePlay !=8)
							|| (bossBoard.dataMap.total == 0 && bossBoard.dataMap.stylePlay != 8)
							|| userBoard.tickTime >= userBoard.dataMap.time )
							&& (userBoard.dataMap.stylePlay !=8 && bossBoard.dataMap.stylePlay!=8 && bossBoard.isPlaying && userBoard.isPlaying )) {
							
//							System.out.println(
//									userBoard.dataMap.total+","+
//									bossBoard.dataMap.total+","+
//									userBoard.tickTime+","+
//									userBoard.dataMap.time+","+
//									userBoard.dataMap.stylePlay+","+
//									bossBoard.isPlaying+","+
//									userBoard.isPlaying);
														
							ctr.a.bgSong.pause();
							ctr.a.mainSong.pause();
							userBoard.isPlaying = false;
							bossBoard.isPlaying = false;
							isPlaying = false;
							isFinishGame = true;
							isWaitResult = true;
							if ((userBoard.pika) >= (bossBoard.pika)) {
								ctr.androidHelper.sendEndGameVS(stage, tweenManager, ctr.d.nameB, true,
										new IMAction() {
											
											@Override
											public void action() {
												// TODO Auto-generated method stub
												final ChangeListener nextLis = new ChangeListener() {
													@Override
													public void changed(ChangeEvent arg0,
															Actor arg1) {
														// TODO Auto-generated method stub
														randLevel(true);
														startGame();
													}
												};
												final ChangeListener replayLis = new ChangeListener() {
													@Override
													public void changed(ChangeEvent arg0,
															Actor arg1) {
														// TODO Auto-generated method stub
														startGame();
													}
												};

												final ChangeListener menuLis = new ChangeListener() {
													@Override
													public void changed(ChangeEvent arg0,
															Actor arg1) {
														// TODO Auto-generated method stub
														ctr.a.bgSong.pause();
														ctr.a.mainSong.pause();
														ctr.a.clearGame();
														clear();
														ctr.a.clearMusic2();
														ctr.a.loadedVS();
														getController().setScreen(getController().getScreen("VSScreen"));
													}
												};
												
												
												if(true){
													postRunnable(new Runnable() {
														
														@Override
														public void run() {
															// TODO Auto-generated method stub		
															ctr.stageHelper.createDialogWinVS(stage,
																	tweenManager, ctr.d.name, ctr.d.nameB,
																	userBoard.pika*75, ctr.d.experUp,
																	bossBoard.pika*75, ctr.d.experUpB,
																	menuLis, replayLis, nextLis);
																if(!ctr.d.coupon.equals("null")){
																ctr.stageHelper.createDialog(stage, tweenManager, ctr.lang.getGiftLevelUp(ctr.d.name, ctr.d.achievement, ctr.d.coupon), 2,
																		new ChangeListener() {
																			
																			@Override
																			public void changed(ChangeEvent arg0, Actor arg1) {
																				// TODO Auto-generated method stub
																				ctr.stageHelper.createDialogCoupon(stage, tweenManager, ctr.d.coupon);
																			}
																		},
																		new ChangeListener() {
																			
																			@Override
																			public void changed(ChangeEvent arg0, Actor arg1) {
																				// TODO Auto-generated method stub
																																			}
																		});
																}
															
															
														}
													});
												}	
											}
										},
										new IMAction() {											
											@Override
											public void action() {
												// TODO Auto-generated method stub												
												postRunnable(new Runnable() {
													
													@Override
													public void run() {
														// TODO Auto-generated method stub
														ctr.a.bgSong.pause();
														ctr.a.mainSong.pause();
														ctr.a.clearGame();
														clear();
														ctr.a.clearMusic2();
														ctr.a.loadedVS();
														getController().setScreen(getController().getScreen("VSScreen"));
													}
												});												
											}
										});							
							} else {
								ctr.androidHelper.sendEndGameVS( stage, tweenManager, ctr.d.nameB, false,
										new IMAction() {											
											@Override
											public void action() {
												// TODO Auto-generated method stub
												final ChangeListener replayLis = new ChangeListener() {
													@Override
													public void changed(ChangeEvent arg0,
															Actor arg1) {
														// TODO Auto-generated method stub
														startGame();
													}
												};

												final ChangeListener menuLis = new ChangeListener() {
													@Override
													public void changed(ChangeEvent arg0,
															Actor arg1) {
														// TODO Auto-generated method stub	
														ctr.a.bgSong.pause();
														ctr.a.mainSong.pause();
														ctr.a.clearGame();
														clear();
														ctr.a.clearMusic2();
														ctr.a.loadedVS();
														getController().setScreen(getController().getScreen("VSScreen"));
													}
												};
												if(true){
													postRunnable(new Runnable() {
														
														@Override
														public void run() {
															// TODO Auto-generated method stub
															ctr.stageHelper.createDialogLoseVS2(stage,
																	tweenManager,ctr.d.name,ctr.d.nameB,userBoard.pika*75,ctr.d.experUp,bossBoard.pika*75, ctr.d.experUpB,menuLis,replayLis);
															if(!ctr.d.coupon.equals("null")){
																ctr.stageHelper.createDialog(stage, tweenManager,ctr.lang.getGiftLevelUp(ctr.d.name, ctr.d.achievement, ctr.d.coupon), 2,
																		new ChangeListener() {
																			
																			@Override
																			public void changed(ChangeEvent arg0, Actor arg1) {
																				// TODO Auto-generated method stub
																				ctr.stageHelper.createDialogCoupon(stage, tweenManager, ctr.d.coupon);
																			}
																		},
																		new ChangeListener() {
																			
																			@Override
																			public void changed(ChangeEvent arg0, Actor arg1) {
																				// TODO Auto-generated method stub
																																			}
																		});
																}
														}
													});
												}
											}
										},
										new IMAction() {
											
											@Override
											public void action() {
												// TODO Auto-generated method stub
												postRunnable(new Runnable() {
													
													@Override
													public void run() {
														// TODO Auto-generated method stub
														ctr.a.bgSong.pause();
														ctr.a.mainSong.pause();
														ctr.a.clearGame();
														clear();
														ctr.a.clearMusic2();
														ctr.a.loadedVS();
														getController().setScreen(getController().getScreen("VSScreen"));
													}
												});
											}
										});
								
															}
						}
					}
				}
				@Override
				public boolean isDetachSelf() {
					// TODO Auto-generated method stub
					return false;
				}

			};
			registerUpdateHandler(check);
		}
		Table avatar = ctr.stageHelper.createAvatar(this, stage, tweenManager, (0), (480-99), 1f);
		stage.addActor(avatar);
		Table ai = createAvatar(this, stage, tweenManager, 560, 100, 1f);
		stage.addActor(ai);
	}	
	
	public void pause(){
		super.pause();
		
		if(isFinishGame==true)
			return;			
		
		ctr.a.bgSong.pause();
		ctr.a.mainSong.pause();
		userBoard.isPlaying = false;
		bossBoard.isPlaying = false;
		isPlaying = false;
		isFinishGame = true;
		isWaitResult = true;
		
		ctr.androidHelper.sendEndGameVS(stage, tweenManager, ctr.d.nameB, false,
				new IMAction() {											
					@Override
					public void action() {
						// TODO Auto-generated method stub
						final ChangeListener replayLis = new ChangeListener() {
							@Override
							public void changed(ChangeEvent arg0,
									Actor arg1) {
								// TODO Auto-generated method stub
								startGame();
							}
						};

						final ChangeListener menuLis = new ChangeListener() {
							@Override
							public void changed(ChangeEvent arg0,
									Actor arg1) {
								// TODO Auto-generated method stub	
								ctr.a.bgSong.pause();
								ctr.a.mainSong.pause();
								ctr.a.clearGame();
								clear();
								ctr.a.clearMusic2();
								ctr.a.loadedVS();
								getController().setScreen(getController().getScreen("VSScreen"));
							}
						};
						if(true){
							postRunnable(new Runnable() {
								
								@Override
								public void run() {
									// TODO Auto-generated method stub
									ctr.stageHelper.createDialogLoseVS2(stage,
											tweenManager,ctr.d.name,ctr.d.nameB,userBoard.pika*75,ctr.d.experUp,bossBoard.pika*75, ctr.d.experUpB,menuLis,replayLis);
									if(!ctr.d.coupon.equals("null")){
										ctr.stageHelper.createDialog(stage, tweenManager,ctr.lang.getGiftLevelUp(ctr.d.name, ctr.d.achievement, ctr.d.coupon), 2,
												new ChangeListener() {
													
													@Override
													public void changed(ChangeEvent arg0, Actor arg1) {
														// TODO Auto-generated method stub
														ctr.stageHelper.createDialogCoupon(stage, tweenManager, ctr.d.coupon);
													}
												},
												new ChangeListener() {
													
													@Override
													public void changed(ChangeEvent arg0, Actor arg1) {
														// TODO Auto-generated method stub
																													}
												});
										}
								}
							});
						}
					}
				},
				new IMAction() {
					
					@Override
					public void action() {
						// TODO Auto-generated method stub
						postRunnable(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								ctr.a.bgSong.pause();
								ctr.a.mainSong.pause();
								ctr.a.clearGame();
								clear();
								ctr.a.clearMusic2();
								ctr.a.loadedVS();
								getController().setScreen(getController().getScreen("VSScreen"));
							}
						});
					}
				});		
	}
	
	public void backKeyProcess(){
		ChangeListener okLis = new ChangeListener() {
			@Override
			public void changed(ChangeEvent event, Actor actor) {				
				
			    if(isFinishGame == false){
			    	ctr.a.bgSong.pause();
					ctr.a.mainSong.pause();
					userBoard.isPlaying = false;
					bossBoard.isPlaying = false;
					isPlaying = false;
			    	isFinishGame = true;
			    	isWaitResult = true;
				ctr.androidHelper.sendEndGameVS(stage, tweenManager, ctr.d.nameB, false, new IMAction() {
					
					@Override
					public void action() {
						// TODO Auto-generated method stub
						
						postRunnable(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								ctr.a.bgSong.pause();
								ctr.a.mainSong.pause();
								clear();
								ctr.a.clearGame();
								ctr.a.clearMusic2();
								ctr.a.loadedVS();
								mController.setScreen(mController.getScreen("VSScreen"));	
							}
						});										
					}
				}, 
				new IMAction() {
					
					@Override
					public void action() {
						// TODO Auto-generated method stub
						postRunnable(new Runnable() {
							
							@Override
							public void run() {
								// TODO Auto-generated method stub
								ctr.a.bgSong.pause();
								ctr.a.mainSong.pause();
								clear();
								ctr.a.clearGame();
								ctr.a.clearMusic2();
								ctr.a.loadedVS();
								mController.setScreen(mController.getScreen("VSScreen"));	
							}
						});				
					}
				});				
			    }
			}
		};
		if(ctr.a.isHasDialog() == false)
			ctr.stageHelper.createDialogOutVS(stage, tweenManager,ctr.lang.backGameConfirm, 2, okLis, null);
	}

	
	Table infoUserFrame;
	Image avatar;
	Label moneyStr;
	
	
	public Table createAvatar(final BaseGameScreen screen, final Stage stage, final TweenManager tweenManager, float x, float y, float scale){						
		infoUserFrame = ctr.stageHelper.createTableWithTransBg(ctr.g.mSkin, 0, 0, (244), (99));	
		avatar = ctr.stageHelper.createImage(ctr.a.avatarBoss, (10), (13),(72), (72));
		infoUserFrame.addActor(ctr.stageHelper.createImage(ctr.a.avatarFrame, 0, 0,(244), (99)));
		infoUserFrame.addActor(avatar);	
		infoUserFrame.addActor(ctr.stageHelper.createImage(ctr.a.boderAvatar, 6, 9, 72+8, 72+8));
		
		moneyStr = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
		
		moneyStr.setText(ctr.d.gomeB+"");
		moneyStr.setPosition(110, 47);
		
		Label nameStr = ctr.stageHelper.createLabel(ctr.g.mFont, "", Color.WHITE, 0, 0);
		if(ctr.d.nameB.length()>10){
			nameStr.setText(ctr.d.nameB.substring(0, 5)+"..."+ctr.d.nameB.substring(ctr.d.nameB.length()-2, ctr.d.nameB.length()));
		}
		else
			nameStr.setText(ctr.d.nameB);
		nameStr.setPosition(87, 77);
		
		Image diamond = ((ctr.stageHelper.createImage(ctr.a.iconGomeMoney, (87), (27),(38), (41))));
		infoUserFrame.addActor(diamond);
		
		infoUserFrame.addActor(moneyStr);
		infoUserFrame.addActor(nameStr);
		
		infoUserFrame.setPosition(x, y);
		infoUserFrame.setVisible(true);		
		return infoUserFrame;
	}
}
